import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-13
 * Time: 18:21
 */
public class MyPriorityQueue {
    public int[] array = new int[10];
    public int usedSize;


    //初始化
    public void initHeap(int[] str) {
        for (int i = 0; i < str.length; i++) {
            array[i] = str[i];
            usedSize++;
        }
    }
    //大根堆
    public void createHeap1(){
        //将每一棵子树都进行调整
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0 ; parent--) {
            //调整的具体代码
            shiftDown1(parent,usedSize);
        }
    }
    //
    public void shiftDown1(int parent,int usedSize){
        int child = parent * 2 + 1;
        while (child < usedSize){
            if(child + 1 < usedSize && array[child] < array[child + 1]){
                child++;
            }
            if (array[child] > array[parent]){
                swap(child,parent);
                parent = child;
                child = child * 2 + 1;
            }else {
                break;
            }
        }
    }
    //小根堆
    public void createHeap2(){
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0 ; parent--) {
            shiftDown2(parent,usedSize);
        }
    }
    //
    public void shiftDown2(int parent,int usedSize){
        int child = parent * 2 + 1;
        while (child < usedSize){
            if(child + 1 < usedSize && array[child] > array[child + 1]){
                child++;
            }
            if (array[child] < array[parent]){
                swap(child,parent);
                parent = child;
                child = child * 2 + 1;
            }else {
                break;
            }
        }
    }
    //
    public void swap(int i,int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    //
    public void disPlay(){
        if (usedSize == 0) System.out.println("堆为空");
        for (int i = 0; i < usedSize; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    //添加
    public void offer(int data){
        if(usedSize == 0) array[0] = data;
        if (isFull()){
            this.array = Arrays.copyOf(array,array.length*2);
        }
        //违法法
        array[usedSize] = data;
        //向上调整

        shiftUp(usedSize);
        usedSize++;
    }
    //向上调整
    public void shiftUp(int child){
        int parent = (child - 1) / 2;
        while (child > 0) {
            if (array[child] > array[parent]) {
                swap(parent, child);
                child = parent;
                parent = (parent - 1) / 2;
            }else {
                break;
            }
        }
    }
    //
    public Boolean isFull(){
        return usedSize == array.length;
    }
    //删除根
    public int pollRoot(){
        if (usedSize == 0){
            System.out.println("堆为空");
            return -1;
        }
        int ret = array[0];
        //将根节点和最后一个节点交换
        swap(0,usedSize-1);
        //useSize--,就相当于把最后一个节点删除
        usedSize--;
        //向下调整
        shiftDown1(0,usedSize);
        return ret;
    }
}